package com.mxx.common.websecurity;

import com.mxx.tomcat.ResponseWriter;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

import javax.servlet.ServletException;
import java.io.IOException;

/**
 * @Auther: Mr. Zeng
 * @Date: 2020/7/21 14:46
 * @Description: 暂无描述
 */
public class AuthenticationResponseBodySessionExpiredHandler implements SessionInformationExpiredStrategy {

    private ResponseWriter responseWriter;
    private AuthenticationBodyHandler authenticationBodyHandler;

    public AuthenticationResponseBodySessionExpiredHandler(ResponseWriter responseWriter, AuthenticationBodyHandler authenticationBodyHandler) {
        this.responseWriter = responseWriter;
        this.authenticationBodyHandler = authenticationBodyHandler;
    }

    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent sessionInformationExpiredEvent) throws IOException, ServletException {
        Object responseBody = authenticationBodyHandler.responseBodyOfSessionExpired(sessionInformationExpiredEvent);
        responseWriter.writer(sessionInformationExpiredEvent.getResponse(), responseBody);
    }
}
